<?php

class Academico_DashboardController extends Zend_Controller_Action {

    private $userInfo;

    //private $_debugLogger;

    public function init() {
        $this->view->errorDiv = "";
    }

    public function preDispatch() {
        // Autenticacion del usuario
        $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity()) {
            $this->_helper->layout->disableLayout();
            $this->_forward("index", "index", "");
        } else {
            $identity = $auth->getIdentity();
            $this->userInfo['username'] = $identity['username'];
            if ($identity['tipo_usuario'] == VirtualLabs_Admin_Usuario::ROL_ADMIN) {
                $this->_helper->layout->disableLayout();
                $this->_redirect('/');
            }
        }
        //fin de la autenicacion del usuario
    }

    public function indexAction() {
        try {
            $machineDao = new VirtualLabs_Academico_Laboratorio();
            $vm = $machineDao->getMachinesByUsername($this->userInfo['username']);
            $labs = $machineDao->getLabsByUsername($this->userInfo['username']);
            $cantidad_vm = count($vm);
            $cantidad_labs = count($labs);
            $this->view->cantidadVM = $cantidad_vm;
            $this->view->cantidadLabs = $cantidad_labs;
        } catch (Exception $e) {
            $this->_errorLog = Zend_Registry::get(VirtualLabs_Common::ERROR_LOG);
            $this->_errorLog->err($e->getMessage());
            $this->_errorLog->err($e->getTraceAsString());
            $this->view->errorDiv = "<div class=\"alert alert-danger alert-dismissable\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">&times;</button> <span><strong>Error:</strong>" . $e->getMessage() . "</span></div>";
        }
    }

    private function myLabs() {
        $lab = new VirtualLabs_Academico_Laboratorio();
        $this->view->myLabsTitle = "<h2>Mis laboratorios</h2>";
        $labs = $lab->getLabsByUsername($this->userInfo['username']);

        $labsToLog = print_r($labs, true);
        $this->debugLog->debug($labsToLog);
        $rowCount = 0;
        $rows = "";
        $more = "<tr><td></td><td><a href='{$this->view->baseUrl()}/academico/laboratorioalumno'>m&aacute;s...</a></td></tr>";
        foreach ($labs as $value) {
            $profesores = $lab->getProfesoresByLab($value['codigo_laboratorio']);
            $losProfe = "<ul class=''>";
            foreach ($profesores as $profe) {
                $losProfe .= "<li class='text-info'><a href='{$this->view->baseUrl()}/academico/usuario/view/username/{$profe['username']}'>{$profe['nombre']}</a></li>";
            }
            $losProfe .= "</ul>";
            if ($rowCount < 3) {
                $rows.="<tr><td>";
                $rows.="<a id='{$value['codigo_laboratorio']}' href='" . $this->view->url(array('module' => 'academico', 'controller' => 'laboratorioalumno', 'action' => 'lab', 'codigo_laboratorio' => $value['codigo_laboratorio']));
                $rows .= "'><img src='" . $this->view->baseUrl() . "/images/virtual-machine-icon.png" . "'/>{$value['nombre_mostrar']}</a></td>";
                $rows .= "<td><p class='text-info'><strong>&Aacute;rea academica:</strong> {$value['nombre_area_academica']}</p><p class='text-info'><strong>Profesor:</strong> $losProfe</p></td></tr>";
                $rowCount++;
            } else {
//                solo se envian los primeros 3 labs
                break;
            }
        }

        $this->view->myLabs = $rows . $more;
    }

    private function myVms() {
        $machineDao = new VirtualLabs_Academico_Machine();
        $this->view->myVmsTitle = "<h2>Mis m&aacute;quinas virtuales</h2>";
        $machines = $machineDao->getVmsByUsername($this->userInfo['username']);
        $machinesToLog = print_r($machines, true);
        $this->debugLog->debug($machinesToLog);
        $rowCount = 0;
        $rows = "";

        foreach ($machines as $value) {
            if ($rowCount < 5) {
                if (($rowCount % 2) == 0) {
                    $rows.="<tr>";
                }
                $this->debugLog->debug("codigodemierda:" . $value['codigo_vm']);
                $cloudData = $machineDao->getVmData($value['codigo_vm']);
                // vm intro
                $rows.="<td>";
                $rows.="<a id='{$value['codigo_maquina_virtual']}' href='" . $this->view->url(array('module' => 'academico', 'controller' => 'maquinaalumno', 'action' => 'vm', 'codigo_maquina_virtual' => $value['codigo_maquina_virtual']));
                $rows .= "'><img src='" . $this->view->baseUrl() . "/images/virtual-machine-icon.png" . "'/>{$cloudData['nombre_mostrar']}</a></td>";

                // vm info
                $rows .= "<td><h4>{$cloudData['displayname']}</h4><p class='text-info'><strong>D&iacute;as a la semana:</strong> {$value['dias_encendido']}</p>" .
                        "<p class='text-info'><strong>Horario por d&iacute;a:</strong> {$value['hora_inicio_dia']} - {$value['hora_fin_dia']}</p>" .
                        "<p class='text-info'><strong>Uso fuera de horario:</strong> {$value['permite_fuera_horario']}</p>" .
                        "</td>";

                if ($rowCount % 2 == 0) {
                    $rowCount++;
                } else {
                    $rows .= "</tr>";
                }
            } else {
                //                solo se envian los primeros 3 labs
                break;
            }
        }
        $more = "<tr><td></td><td></td><td></td><td><a href='{$this->view->baseUrl()}/academico/maquinasalumno'>m&aacute;s...</a></td></tr>";
        $this->view->myVms = $rows . $more;
    }

    public function mymachinesAction() {
        $auth = Zend_Auth::getInstance();
        $userInfo = $auth->getIdentity();
        Zend_Debug::dump($userInfo);
        $machineDao = new VirtualLabs_Academico_Laboratorio();
        $machineDao->getMachinesByUsername($username);
    }

}

